package com.rf.richfitwheel.outbill.controller;


import com.rf.richfitwheel.admin.utils.UserUtil;
import com.rf.richfitwheel.common.utils.StringUtils;
import com.rf.richfitwheel.outbill.dao.ReceiptBillInfoMapper;
import com.rf.richfitwheel.outbill.model.OutBillInfo;
import com.rf.richfitwheel.outbill.model.ReceiptBillInfo;
import com.rf.richfitwheel.outbill.param.ReceiptBillInfoParam;
import com.rf.richfitwheel.outbill.service.ReceiptBillInfoService;
import com.rf.richfitwheel.common.Param;
import com.rf.richfitwheel.utils.PageJson;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lihong
 * @since 2024-06-19
 */
@Api(tags = "提示收票")
@RestController
@RequestMapping("/outbill/receiptBillInfo")
public class ReceiptBillInfoController {

    @Autowired
    private ReceiptBillInfoService receiptBillInfoService;

    /**
     * 通过ID查询信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "详情接口")
    @ApiImplicitParam(dataType = "String", name = "id", value = "主键Id", required = true)
    @GetMapping("/{id}")
    public ReceiptBillInfo detail(@PathVariable String id) {
        QueryWrapper<ReceiptBillInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        queryWrapper.select(ReceiptBillInfo.class, v -> !(v.getColumn().equals("update_time") || v.getColumn().equals("create_time")));
        ReceiptBillInfo receiptBillInfo = receiptBillInfoService.getOne(queryWrapper);
        return receiptBillInfo;
    }

    /**
     * 分页查询信息
     *
     * @param
     * @return
     */
    @ApiOperation(value = "分页查询")
    @PostMapping("/page")
    public PageJson getPageList(@RequestBody ReceiptBillInfoParam receiptBillInfo) {
        return receiptBillInfoService.pageJson(receiptBillInfo);
    }

    /**
     * 添加
     * @param receiptBillInfo
     * @return
     */
    @ApiOperation(value = "暂存")
    @PostMapping("/add")
    public boolean save(@RequestBody ReceiptBillInfo receiptBillInfo) {
        if(StringUtils.isNotBlank(receiptBillInfo.getId())){
            return receiptBillInfoService.updateById(receiptBillInfo);
        }else{
            return receiptBillInfoService.save(receiptBillInfo);
        }
    }

    /**
     * 提示出票提交
     *
     * @param outBillInfo
     * @return
     */
    @ApiOperation("提示出票提交")
    @PostMapping("/addFlow")
    public boolean addFlow(@RequestBody @ApiParam("receiptBillInfo和wfTaskParam对象") Map<String, Object> params) {
        return receiptBillInfoService.addFlow(params);
    }

    /**
     * 撤销提示出票走流程
     *
     * @param outBillInfo
     * @return
     */
    @PostMapping("/backFlow")
    @ApiOperation("撤回提示出票提交接口")
    public boolean backFlow(@RequestBody @ApiParam("param传receiptBillInfo和wfTaskParam对象") Map<String, Object> params) {
        return receiptBillInfoService.backFlow(params);
    }

    /**
     * 撤销提示出票更新走流程
     *
     * @param modifyFlow
     * @return
     */
    @PostMapping("/backModifyFlow")
    @ApiOperation("撤销提示出票更新方法，审批人走流程使用")
    public boolean backModifyFlow(@RequestBody @ApiParam("param传receiptBillInfo和wfTaskParam对象") Map<String, Object> params) {
        return receiptBillInfoService.backModifyFlow(params,UserUtil.getUser());
    }

    /**
     * 修改
     * @param receiptBillInfo
     * @return
     */
    @ApiOperation(value = "修改")
    @PostMapping("/modify")
    public boolean modify(@RequestBody ReceiptBillInfo receiptBillInfo) {
        return receiptBillInfoService.updateById(receiptBillInfo);
    }

    /**
     * 提示承兌更新走流程
     *
     * @param modifyFlow
     * @return
     */
    @PostMapping("/modifyFlow")
    @ApiOperation("提示出票更新方法，审批人走流程使用")
    public boolean modifyFlow(@RequestBody @ApiParam("param传receiptBillInfo和wfTaskParam对象") Map<String, Object> params) {
        return receiptBillInfoService.modifyFlow(params, UserUtil.getUser());
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除")
    @PostMapping("/del")
    public boolean delBatch(@RequestParam("ids") List<String> ids) {
        return receiptBillInfoService.removeByIds(ids);
    }

    /**
     * 提示出票作废
     */
    @ApiOperation("作废")
    @GetMapping("/cancel/{id}")
    public Boolean cancel(@PathVariable("id") @ApiParam("主键id") String id) {
        QueryWrapper<ReceiptBillInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        ReceiptBillInfo receiptBillInfo = receiptBillInfoService.getOne(queryWrapper);
        receiptBillInfo.setApvst("6");
        return receiptBillInfoService.updateById(receiptBillInfo);
    }
}
